<?php
    class DBManager extends CI_Model{

     var $prods;
     var $prodPrice;
    
    public function __construct(){
          parent::__construct();
         
           $this->load->database();

      }  


      public function get_ad_types(){
          $query = $this->db->query("SELECT id,name FROM AdType");

          return $query->result_array();
      }
      public function get_categories_by_type($type){
          $query = $this->db->query("SELECT tclu.catid,c.name FROM  Category c JOIN TypeCatLU tclu ON c.id = tclu.catid WHERE tclu.typeid=" . $this->db->escape($type));
          return $query->result_array();
      }

      public function get_products_by_type_category($cat,$type){
          $query = $this->db->query("SELECT p.id,p.name,plu.wkday_price,plu.wkend_price FROM Product p JOIN ProductLU plu ON p.id=plu.prodid WHERE p.catid=" . $this->db->escape($cat) . " AND plu.typeid=" . $this->db->escape($type) );
          return $query->result_array();
      }
      
      
      function  get_product_details($type,$prod){
          //return product given the id
          $query = $this->db->query("SELECT p.id,p.name,plu.wkday_price,plu.wkend_price FROM Product p JOIN ProductLU plu ON p.id=plu.prodid WHERE plu.prodid=" . $this->db->escape($prod) . " AND plu.typeid=" . $this->db->escape($type) );
          return $query->result_array();
     
      }

      function get_next_cartid(){
          $format = "CART-" . date("Y-m-d") . "-%1$05d";
          $flag=FALSE;
          $query = $this->db->query("SELECT MAX(CartSequence) as next_seq FROM Uniques");
          $nextseq=0;
          foreach($query->result_array() as $r){
              $nextseq= $r['next_seq'];
          }
          //increment sequence         
           $nextid = sprintf($format,++$nextseq);
        
          //upate uniques table
          $query = $this->db->query("UPDATE Uniques SET CartSequence='" . $this->db->escape($nextseq) ."'");
          
          return $nextid;

      }

      function add_to_cart($arr){

          //check if cartid exists
          //remove this,dont check if a cart exist, they can be re use
          $uid = $arr['userid'];
          $pid = $arr['prodid'];
          $typeid = $arr['typeid'];
          $cartid=$arr['cartid'];
          $sessionid='';
           
          $query = $this->db->query("SELECT Count(*) as cnt,cartid FROM AdCart WHERE userid='{$uid}' AND prodid='{$pid}' AND typeid='{$typeid}' AND cartid='{$cartid}'");
          $cnt=0;
          foreach($query->result_array() as $r){
              $cnt = $r['cnt'];
             
          }
          
         if($cnt<1){
               //create new cart
               //$cartid = $this->get_next_cartid();
               $cart = array(
                        'cartid'=> $cartid,
                        'typeid'=> $arr['typeid'],
                        'userid'=>$arr['userid'],
                        'prodid'=>$arr['prodid'],
                        'sessionid'=>$sessionid,
                        'adsize'=>$arr['adsize'],
                        'from_date'=>$arr['from_date'],
                        'to_date'=>$arr['to_date'],
                        'adtitle'=>$arr['adtitle'],
                        'addesc'=>$arr['addesc'],
                        'adfile'=>$arr['adfile'],
                        'total'=>$arr['total'] );

               $query =$this->db->set($cart);
               $query=$this->db->insert('AdCart');
               //set cart id in session
                $this->session->set_userdata('cartid',$cartid);
           }else{
              //update cart
              $cart = array(
                        'typeid'=> $arr['typeid'],
                        'userid'=>$arr['userid'],
                        'prodid'=>$arr['prodid'],
                        'sessionid'=>$sessionid,
                        'adsize'=>$arr['adsize'],
                        'from_date'=>$arr['from_date'],
                        'to_date'=>$arr['to_date'],
                        'adtitle'=>$arr['adtitle'],
                        'addesc'=>$arr['addesc'],
                        'adfile'=>$arr['adfile'],
                        'total'=>$arr['total'],
                        'date_updated'=> date("Y-m-d H:i:s") );

               //$query =$this->db->where('cartid',$cartid);
               //$query =$this->db->where('sessionid',$sessionid);
               $query =$this->db->where('prodid',$pid);
               $query =$this->db->where('typeid',$typeid);
               $query =$this->db->where('cartid',$cartid);
               $query =$this->db->where('userid',$uid);


               $query=$this->db->update('AdCart',$cart);

          }
         
          return $cartid;


      }
      function delete_item_from_cart($prod,$type,$user){
          try{

              $query = $this->db->query("DELETE FROM AdCart WHERE userid='{$user}' AND prodid='{$prod}' AND typeid='{$type}'");
              
          }catch(Exception $e){
              echo $e;
          }
      }
      
     

      function get_adtypes(){
          try{
             

               $query = $this->db->get('AdType');
              foreach($query->result() as $row){
                  echo $row->name . "<br/>";
              }
              
          }catch(Exception $e){
              echo $e;
          }
         
      }

      function get_user_cart($user,$typeid){
          
          try{
               $query = $this->db->query("SELECT prodid,adsize,adtitle,addesc,from_date,to_date,total FROM AdCart WHERE userid='{$user}' AND typeid='{$typeid}'");
               return $query;

          }catch(Exception $e){
              
          }
      }


    }


?>

